<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>修改菜单</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form"lay-filter="menu-form">
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">上级菜单</label>
        <div class="layui-input-block">
            <input type="text" name="parentName" placeholder="请选择上级菜单" value="" class="layui-input" readonly>
            <div id="menuIds"></div>
            <input type="text" name="parentId" value="" hidden>
            <input type="text" name="menuId" value="" hidden>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">菜单名称</label>
        <div class="layui-input-block">
            <input type="text" name="menuName" lay-verify="required" lay-reqtext="菜单名称不能为空" placeholder="请填写菜单名称" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">跳转地址</label>
        <div class="layui-input-block">
            <input type="text" name="url" placeholder="请填跳转地址" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">菜单图标</label>
        <div class="layui-input-block">
            <input type="text" name="icon" placeholder="请填写菜单图标" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">排序</label>
        <div class="layui-input-block">
            <input type="text" name="orderNum" placeholder="请填写菜单排序" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <select name="visible">
                <option value="0">正常</option>
                <option value="1">停用</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea name="remark" class="layui-textarea" placeholder="请填写备注信息"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../lib/common/common.js" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script>

    window.onload=function(){
        let id = GetQueryString("id");
        let parentId = GetQueryString("parentId");
        if(isNotNull(id) && isNotNull(parentId)){
            $('input[name="menuId"]').val(id);
            $('input[name="parentId"]').val(parentId);
            getMenuNameById(parentId);
            init(id);
        }else{
            var iframeIndex = parent.layer.getFrameIndex(window.name);
            parent.layer.close(iframeIndex);
        }
    }

    /**
     * 初始化数据
     */
    function init(id) {
        $.ajax({
            type:"post",
            url:'/menu/edit/'+id,
            dataType:'json',
            contentType: 'application/x-www-form-urlencoded',
            success:function(req){
                layui.use(['form'], function () {
                    let form = layui.form
                    form.val('menu-form',{
                        "menuName": req.data.menuName,
                        "url": req.data.url,
                        "icon": req.data.icon,
                        "orderNum": req.data.orderNum,
                        "visible": req.data.visible,
                        "remark": req.data.remark
                    });
                })
            },
            error:function(e){
                layer.msg("数据有误");
            }
        })
    }

    layui.use(['layer', 'form', 'layarea', 'tree'], function () {
        var form = layui.form,
            layer = layui.layer,
            layarea = layui.layarea,
            tree = layui.tree,
            $ = layui.$;

        tree.render({
            elem: '#menuIds'  //绑定元素
            ,id: 'menuTreeId'
            ,showCheckbox: false
            ,checkChild: false
            ,data: getTreeData()
            ,click: function(obj){
                console.log(obj.data); // 得到当前点击的节点数据
                $('input[name="parentName"]').val(obj.data.title);
                $('input[name="parentId"]').val(obj.data.id);
            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index = layer.alert("确定提交吗？", {
                title: '提示'
            }, function () {
                let myMsg = layer.msg("正在提交...",{
                    icon:16,
                    time:-1
                })
                $.ajax({
                    type:"post",
                    url:'/menu/edit',
                    data:JSON.stringify(data.field),
                    dataType:'json',
                    contentType: 'application/json; charset=UTF-8',
                    success:function(req){
                        layer.msg("修改成功");
                        layer.close(index);
                        // 关闭弹出层
                        setTimeout(function () {
                            layer.close(myMsg);//手动关闭
                            let iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        }, 1500);
                    },
                    error:function(e){
                        layer.msg("出错啦");
                        layer.close(myMsg);//手动关闭
                    }
                })

            });

            return false;
        });

    });

    function getTreeData() {
        let data;
        let obj = {}
        $.ajax({
            type:"post",
            url:'/menu/roleMenuTreeData',
            data:JSON.stringify(obj),
            dataType:'json',
            contentType: 'application/json; charset=UTF-8',
            async: false,
            success:function(req){
                data = req.data
            },
            error:function(e){
                alert("出错啦！")
            }
        })
        return data;
    }

    // 获取选中节点的id
    function getChecked_list(data) {
        var id = "";
        $.each(data, function (index, item) {
            if (id != "") {
                id = id + "," + item.id;
            }
            else {
                id = item.id;
            }
            var i = getChecked_list(item.children);
            if (i != "") {
                id = id + "," + i;
            }
        });
        return id;
    }

    //根据菜单id获取菜单名称
    function getMenuNameById(menuId) {
        $.ajax({
            type:"post",
            url:'/menu/edit/'+menuId,
            dataType:'json',
            contentType: 'application/x-www-form-urlencoded',
            success:function(req){
                let menuName = req.data.menuName;
                $('input[name="parentName"]').val(menuName);
            },
            error:function(e){
                layer.msg("出错啦！");
            }
        })
    }

</script>
</body>
</html>
